package team.seekers.catering.community.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import team.seekers.catering.community.entity.dao.FirstComment;
import team.seekers.catering.community.entity.dto.add.FirstCommentAddDTO;
import team.seekers.catering.community.entity.dto.update.FirstCommentUpdateDTO;
import team.seekers.catering.community.entity.vo.info.FirstCommentInfoVO;
import team.seekers.catering.community.mapper.FirstCommentMapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FirstCommentService extends ServiceImpl<FirstCommentMapper, FirstComment> {
    /**
     * 增加一个一级评论
     */
    public String add(FirstCommentAddDTO firstCommentAddDTO){
        FirstComment firstComment = new FirstComment();
        BeanUtils.copyProperties(firstCommentAddDTO,firstComment);
        save(firstComment);
        return firstComment.getId();
    }

    /**
     * 删除一个一级评论
     */
    public Boolean delete(String id){
        boolean remove = removeById(id);
        return remove;
    }

    /**
     * 修改一个一级评论
     */
    public String update(FirstCommentUpdateDTO firstCommentUpdateDTO){
        FirstComment firstComment = new FirstComment();
        BeanUtils.copyProperties(firstCommentUpdateDTO,firstComment);
        updateById(firstComment);
        return firstComment.getId();
    }

    /**
     * 根据id查询一个一级评论
     */
    public FirstCommentInfoVO get(String id){
        FirstComment firstComment = getById(id);
        FirstCommentInfoVO firstCommentInfoVO = new FirstCommentInfoVO();
        BeanUtils.copyProperties(firstComment,firstCommentInfoVO);
        return firstCommentInfoVO;
    }

    /**
     * 分页查询所有的一级评论
     */
    public Map<String, Object> adminPageFirstComment(long current, long limit, QueryWrapper<FirstComment> wrapper,String id) {
        Page<FirstComment> page = new Page<>(current,limit);
        wrapper.orderByDesc("gmt_create").eq("a_id",id).or().eq("p_id",id);
        page(page,wrapper);
        Long total = page.getTotal();
        if(page.getRecords() == null){
            return null;
        }
        System.out.println("获取的记录是"+page.getRecords());

        List<FirstCommentInfoVO> list = new ArrayList<>();
        for (FirstComment record : page.getRecords()) {
            FirstCommentInfoVO vo = get(record.getId());
            list.add(vo);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rows",list);
        return map;
    }

    /**
     * TODO
     * 根据文章或帖子id查询所有的一级评论
     */
    public List<FirstComment> queryAllCommentById(String id){
        QueryWrapper<FirstComment> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create").eq("a_id",id).or().eq("p_id",id);
        List<FirstComment> firstComments = list(wrapper);
        return firstComments;
    }
}
